<?php

switch($action){
    case 'del_data':
        switch($type){
            case 'DATA_DEL':
                $html_file = $db->result_first("SELECT `url` FROM `product_list` WHERE `id`='{$product_id}'");
                $status = (empty($html_file) || @unlink(APP_ROOT.$html_file))
                       && $db->query("DELETE FROM `product_desc` WHERE `prod_id`='{$product_id}'")
                       && $db->query("DELETE FROM `product_list` WHERE `id`='{$product_id}'")
                        ? 1 : 0;
                break;
            case 'STATUS_DEL':
                $status = $db->query("UPDATE `product_list` SET `status`='DEL' WHERE `id`='{$product_id}'")
                        ? 1 : 0;
                break;
        }
        echo ajax_return($status);
        exit();
        break;
    case 'verify_data':
        $sql = "UPDATE `product_list` SET `status`='".($type == 'OK' ? 'NEED_GENERATE' : 'VERIFY_FAIL')."' WHERE `id`='{$product_id}'";
        echo ajax_return($db->query($sql) ? 1 : 0);
        exit();
        break;
    case 're_data':
        $sql = "UPDATE `product_list` SET `status`='NEED_GENERATE' WHERE `id`='{$product_id}'";
        echo ajax_return($db->query($sql) ? 1 : 0);
        exit();
        break;
    case 'get_data':
        include APP_ROOT.'/include/dataview.class.php';

        $artile_status = array('NEED_GENERATE'=>'<font color="blue">需要生成静态页面</font>','GENERATED'=>'<font color="green">已生成静态页面</font>','NEED_VERIFY'=>'<font color="orange">需要审核</font>','VERIFY_FAIL'=>'<font color="gray">审核未通过</font>','DEL'=>'<font color="red">已删除</font>');

        $cat_ids = $category_list[intval($cat_id)]['childids'];
        $page = $page > 0 ? $page : 1;
        $per_page = $per_page > 0 ? $per_page : 20;
        $row_start = ($page - 1) * $per_page;


        $sql = "SELECT * FROM `product_list` ".(empty($cat_ids) ? '' : "WHERE `cat_id` IN({$cat_ids})");
        $records = $db->result_first(str_replace('*', 'count(*)', $sql));
        $data = $db->fetch_all($sql." ORDER BY `id` DESC LIMIT {$row_start},{$per_page}");
        
        $view = new dataview();
	echo $view
                ->set_data($data)
                ->add_colume('id','ID',50,null,'center')
                ->add_colume('product_sn','货号',150,null,'center')
                ->add_colume('product_name','产品名称',300,callback_name,'center')
                ->add_colume('designer_name','设计师',100,callback_designer,'center')
                ->add_colume('brand_name','品牌',150,callback_brand,'center')
                ->add_colume('create_time','创建时间',130,callback_time2date,'center')
                ->add_colume('update_time','更新时间',130,callback_time2date,'center')
                ->add_colume('status','状态',110,callback_status,'center')
                ->add_colume('id','操作',80,callback_op,'center')
                ->add_footer(get_ajax_page($records, 'get_data', $per_page, $page),'center')
                ->set_table('class','ui-widget ui-widget-content')
                ->set_header('class','ui-widget-header');
        exit();
        break;
    default:
        break;
}

function callback_name($curr,$row){
    return '<span title="[描述] '.$row['product_desc'].'"><a href="'
        .APP_URL
        .($row['status'] == 'GENERATED' ? $row['url'] : '/index.php/product/detail/product_id/'.$row['id'].'.shtml')
        .'" target="_blank">'.mb_substr($curr,0,20,'utf-8').'</span>';
}
function callback_designer($curr,$row){
    return '<span title="'.$row['from_url'].'">'.mb_substr($curr,0,7,'utf-8').'</span>';
}
function callback_brand($curr,$row){
    return mb_substr($curr,0,7,'utf-8');
}
function callback_time2date($curr,$row){
    return $curr == 0 ? '': date('Y-m-d H:i:s',$curr);
}
function callback_status($curr,$row){
    global $artile_status;
    return $artile_status[$curr];
}
function callback_op($curr,$row){
    $op_html = '';
    switch($row['status']){
        case 'NEED_GENERATE':
        case 'GENERATED':
            $op_html .= mk_js_onclick('修改', 'edit_data', $curr,'brown');
            $op_html .= '|';
            $op_html .= mk_js_onclick('删除', 'del_data', array($curr,'STATUS_DEL'),'grey');
            break;
        case 'NEED_VERIFY':
            $op_html .= mk_js_onclick('接受', 'verify_data', array($curr,'OK'),'blueviolet');
            $op_html .= '|';
            $op_html .= mk_js_onclick('拒绝', 'verify_data', array($curr,'NG'),'orange');
            break;
        case 'VERIFY_FAIL':
        case 'DEL':
            $title = $row['status'] == 'DEL' ? '恢复' : '接受';
            $op_html .= mk_js_onclick($title, 're_data', array($curr,$title),'blueviolet');
            $op_html .= '|';
            $op_html .= mk_js_onclick('<span title="完全删除">删除</span>', 'del_data', array($curr,'DATA_DEL'),'red');
            break;
    }
    return $op_html;
}
function mk_js_onclick($title,$js_name,$js_var,$font_color = ''){
    !empty($font_color) && $title = '<font color="'.$font_color.'">'.$title.'</font>';
    return ' <a href="javascript:void(0)" onclick="'.$js_name.'(\''.(is_array($js_var) ? implode("','", $js_var) : $js_var).'\')">'.$title.'</a> ';
}
?>
